package JavaSE.d5;


import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Scanner;
import java.util.stream.Stream;

/*

通过Files工具类测试：
public static boolean isWritable(Path path)
public static boolean exists(Path path)
public static Path createFile(Path path,FileAttribute<?>... attrs)  FileAttribute<?>...可选参数  “参数名...”代表可选参数
public static boolean isSameFile(Path p1,Path p2)
public static long size(Path path)
public static boolean isDirectory(Path path)
public static Path createDirectory(Path dir,FileAttribute<?>... attrs)
public void delete(Path path)
public boolean deleteIfExists()

public static Stream<Path> list(Path dir)
public static DirectoryStream<Path> newDirectoryStream(Path dir)
public static DirectoryStream<Path> newDirectoryStream(Path dir,DirectoryStream.Filter<? super Path> filter))
public static Stream<Path> walk(Path start,FileVisitOption... options)
public static Stream<Path> find(Path start,int maxDepth,BiPredicate<Path,BasicFileAttributes> matcher,FileVisitOption... options)
public static Path walkFileTree(Path start,FileVisitor<? super Path> visitor)
 */

public class Test21 {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        Path p= Path.of("center.txt"); //随意写一个文件字符串路径，作为锚点
        p=p.toAbsolutePath();//转换成绝对路径
        if(Files.exists(p)){//判断文件是否存在
            //存在
            System.out.println( "路径所指示的文件是否可写:"+Files.isWritable(p));//判断路径所指示的文件是否可写。
            System.out.println("center锚点文件大小："+Files.size(p));
            System.out.println("center锚点文件是否是目录"+Files.isDirectory(p));
            System.out.println("获取锚点文件父目录"+p.getParent());
            System.out.println("查看父目录是否是目录"+Files.isDirectory(p.getParent()));
            if(Files.exists(p.getParent().resolve("centertest"))){
                System.out.println("centertest目录已存在");
            }else {
                System.out.println("创建centertest目录");
                Files.createDirectories(p.getParent().resolve("centertest"));//在父目录下创建“centertest目录”
            }
            Path p3=p.getParent().resolve("centertest");
            Files.createDirectories(p3.resolve("con1"));//在centertest目录中创建con1目录
            System.out.println("centertest目录中con1目录创建完成");
            System.out.println("按下任意键继续操作文件");
            scanner.nextLine();
            Files.delete(p3.resolve("con1"));//在centertest目录中删除con1目录
            System.out.println("centertest目录中con1目录删除完成");


        }else {
            //不存在
            Files.createFile(p);//创建文件
            System.out.println("已创建文件");
        }
        Path p2=p;//让p2路径对象复制p

        System.out.println("p和p2路径指代同一个文件否:"+Files.isSameFile(p,p2));//判断p和p2路径指代同一个文件否

        p=p.resolve(p.getParent());//将p指向锚点父节点

        System.out.println();
        System.out.println("------------------------");
        System.out.println("当前p路径所指示：------------>"+p);
        System.out.println("使用流开始遍历p的子文件");
        Stream<Path> list = Files.list(p);//Stream<Path>这是一个能让Path类型数据流过的管道，不存储数据，但能使用流过的数据。
        //  把管道list一个端口指向p路径，开始流动使用,
        list.forEach(e-> System.out.println(e));//把流过的数据输出，这样就遍历了路径p下的所有文件
        System.out.println("------------------------");
        System.out.println();

        System.out.println();
        System.out.println("------------------------");
        System.out.println("使用带过滤器的流开始遍历p的子文件");
        DirectoryStream<Path> paths = Files.newDirectoryStream(p,"*.txt");//Files.newDirectoryStream(路径，);可以带过滤器的的遍历工具
        paths.forEach(e-> System.out.println(e));
        System.out.println("------------------------");
        System.out.println();

        System.out.println();
        System.out.println("------------------------");
        System.out.println("使用带过滤器的流开始遍历p的子文件");
        DirectoryStream<Path> paths0 = Files.newDirectoryStream(p,e->Files.size(e)>1000);//Files.newDirectoryStream(路径，);可以带过滤器的的遍历工具
        //e->e.getFileName().toString().endsWith("*.java"),传入的参数是过滤器泛型接口，这时候e是一个Path对象，返回值布尔型,符合结果的返回
        paths0.forEach(e-> System.out.println(e));
        System.out.println("------------------------");
        System.out.println();

        System.out.println();
        System.out.println("------------------------");
        p=p.resolve("centertest");//将p指向所测试的文件目录
        Stream<Path> walk = Files.walk(p);
        System.out.println("p目录树结构");
        walk.forEach(e-> System.out.println(e));//以p为根节点遍历目录数
        System.out.println("------------------------");
        System.out.println();

        System.out.println();
        System.out.println("------------------------");
        System.out.println("使用find遍历");
        Stream<Path> pathStream = Files.find(p,100,(e,d)->e.toString().endsWith("3"));//找符合条件的目录
        pathStream.forEach(e-> System.out.println(e));
        System.out.println("------------------------");
        System.out.println();


        //遍历目录文件树
        Files.walkFileTree(p, new FileVisitor<Path>() {
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                System.out.println("访问前路径"+dir);
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                System.out.println("访问到文件路径"+file);
                Files.delete(file);
                System.out.println("文件已经删除");
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
                System.out.println("访问失败路径"+file);
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                System.out.println("访问后路径"+dir);
                Files.delete(dir);
                System.out.println("已删除目录");
                return FileVisitResult.CONTINUE;
            }
        });


    }

}
